{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e6685771",
   "metadata": {},
   "source": [
    "# Lesson 3: Translation and Summarization"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c7109da",
   "metadata": {},
   "source": [
    "First, install the following if you are running locally:\n",
    "\n",
    "```\n",
    "    !pip install transformers \n",
    "    !pip install torch\n",
    "```\n",
    "\n",
    "_Note:_ You don't have to install any extra libraries if you are running this lab in this platform."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97e3e7c9-1437-4784-8a21-cd200bc609a5",
   "metadata": {},
   "source": [
    "- Here is some code that suppresses some warning messages."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "782af222-1bea-449a-8dd4-655ad7a7b8ea",
   "metadata": {
    "height": 47
   },
   "outputs": [],
   "source": [
    "from transformers.utils import logging\n",
    "logging.set_verbosity_error()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bea43ec1",
   "metadata": {},
   "source": [
    "### Build the `translation` pipeline using 🤗 Transformers Library"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d1d46ac9-d665-4690-99a4-43b625e02114",
   "metadata": {
    "height": 47
   },
   "outputs": [],
   "source": [
    "from transformers import pipeline \n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "014e1c26-df35-406c-8ac2-9789b011c86b",
   "metadata": {
    "height": 94
   },
   "outputs": [],
   "source": [
    "# Create the translator pipeline using a model from Meta\n",
    "translator = pipeline(task=\"translation\",\n",
    "                      model=\"./models/facebook/nllb-200-distilled-600M\",\n",
    "                      torch_dtype=torch.bfloat16) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69d8f7a5",
   "metadata": {},
   "source": [
    "Note: Find more information about the model 'nllb-200-distilled-600M', [here](https://huggingface.co/facebook/nllb-200-distilled-600M).\n",
    "\n",
    "NLLB: No Language Left Behind"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "095bd1c5-a96f-4b20-8e9c-601b0b158fd8",
   "metadata": {
    "height": 132
   },
   "outputs": [],
   "source": [
    "# Set the text to be translated\n",
    "text = \"\"\"\\\n",
    "My puppy is adorable, \\\n",
    "Your kitten is cute.\n",
    "Her panda is friendly.\n",
    "His llama is thoughtful. \\\n",
    "We all have nice pets!\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "03d9ebdf-86d8-493b-8757-74b3d1010442",
   "metadata": {
    "height": 64
   },
   "outputs": [],
   "source": [
    "text_translated = translator(text,\n",
    "                             src_lang=\"eng_Latn\",\n",
    "                             tgt_lang=\"fra_Latn\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "711052f5",
   "metadata": {},
   "source": [
    "Note: Find more languages's codes, [in this repository.](https://github.com/facebookresearch/flores/blob/main/flores200/README.md#languages-in-flores-200)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f4ba07e3-4a5e-4bf2-86a9-498781828eca",
   "metadata": {
    "height": 47
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'translation_text': 'Mon chiot est adorable, ton chaton est mignon, son panda est ami, sa lamme est attentive, nous avons tous de beaux animaux de compagnie.'}]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Print the translated text\n",
    "text_translated"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7517649",
   "metadata": {},
   "source": [
    "* Before moving with more examples, let's free some memory."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c16e5dad-dac0-42e4-9a87-8128a1d49b44",
   "metadata": {
    "height": 47
   },
   "outputs": [],
   "source": [
    "# import gargabe collector\n",
    "import gc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "43cafb3a-51b8-4aae-929c-31d524dec530",
   "metadata": {
    "height": 47
   },
   "outputs": [],
   "source": [
    "# Delete translator\n",
    "del translator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "61d698a7-8ae2-475e-ac46-d768c282b17c",
   "metadata": {
    "height": 30
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gc.collect()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2fac55f",
   "metadata": {},
   "source": [
    "### Build the `summarization` pipeline using 🤗 Transformers Library"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b132c646-0c6a-4c57-939a-b3015ea4b76f",
   "metadata": {
    "height": 81
   },
   "outputs": [],
   "source": [
    "# Create the summarizer pipeline using a model from Meta\n",
    "summarizer = pipeline(task=\"summarization\",\n",
    "                      model=\"./models/facebook/bart-large-cnn\",\n",
    "                      torch_dtype=torch.bfloat16)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ca6f847",
   "metadata": {},
   "source": [
    "Note: Find more information about the model 'bart-large-cnn', [here](https://huggingface.co/facebook/bart-large-cnn)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "98276d66-4274-4a2f-b6a7-b4fb839b94f7",
   "metadata": {
    "height": 179
   },
   "outputs": [],
   "source": [
    "# Text to be summarized\n",
    "text = \"\"\"Paris is the capital and most populous city of France, with\n",
    "          an estimated population of 2,175,601 residents as of 2018,\n",
    "          in an area of more than 105 square kilometres (41 square\n",
    "          miles). The City of Paris is the centre and seat of\n",
    "          government of the region and province of Île-de-France, or\n",
    "          Paris Region, which has an estimated population of\n",
    "          12,174,880, or about 18 percent of the population of France\n",
    "          as of 2017.\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d856f193-cbf7-450b-8ae3-42287096e56f",
   "metadata": {
    "height": 64
   },
   "outputs": [],
   "source": [
    "summary = summarizer(text,\n",
    "                     min_length=10,\n",
    "                     max_length=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a2c79f81-6baf-4f6b-95ee-b1a2072ec073",
   "metadata": {
    "height": 47
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'summary_text': 'Paris is the capital and most populous city of France, with an estimated population of 2,175,601 residents as of 2018. The City of Paris is the centre and seat of the government of the region and province of Île-de-France.'}]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Print the result of the summarization\n",
    "summary"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca56abc0",
   "metadata": {},
   "source": [
    "### Try yourself! \n",
    "Now, it is your turn! Try this model with your own texts!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
